package com.seo.controller.keyWord;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.seo.function.PageTool;
import com.seo.pojo.ReturnBody;
import com.seo.service.keyword.member.sdk.KeywordGMemberService;
import com.seo.service.keyword.memberE.sdk.KeywordGMemberEService;
import com.seo.service.keyword.sdk.KeywordGService;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: GoodsController
 * @author rtc
 * @version 2018.07.28 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/keyWordGE/")
public class KeyWordGMemberEController {
	
	@Autowired
	private KeywordGMemberEService keywordGMemberEService;
	
	@Autowired
	private KeywordGMemberService keywordGMemberService;
	
	@Autowired
	private KeywordGService keywordGService;
	
	@Autowired
	private PageTool pageTool;

	/*
	 * 关键字列表
	 */
	@RequestMapping("/list")
	public String list(@RequestParam Map<String, Object> pam,HttpServletRequest request, HttpSession session) {
		log.info("进入关键字列表...");
		List<Map<String, Object>> venderList = keywordGMemberService.getVenderList(); //厂商列表
		List<Map<String, Object>> engineList = keywordGService.getEngineList(); //搜索引擎列表
		Integer userId = 0;
		if(pam.containsKey("userId") ) {
			String userIdStr = (String) pam.get("userId");
			if(StringUtils.isNotBlank(userIdStr)) {
				userId = Integer.valueOf(userIdStr);
			}else {
				userId = Jurisdiction.getT("id", Integer.class);
			}
		}else {
			userId = Jurisdiction.getT("id", Integer.class);
		}
		List<Map<String, Object>> groupList =  keywordGMemberService.getGroupByUserId(userId); //所在分组
		pam.put("userId", userId);
		//排名区间 1：1-10 2：11-50 3： 51-100 4: 100+
		String rankingInterval = (String) pam.get("rankingInterval");
		if(StringUtils.isNotBlank(rankingInterval)) {
			String startRanking = null;
			String endRanking = null;
			if("1".equals(rankingInterval)) {
				startRanking = "1";
				endRanking = "10";
			}else if("2".equals(rankingInterval)) {
				startRanking = "11";
				endRanking = "50";
			}else if("3".equals(rankingInterval)) {
				startRanking = "51";
				endRanking = "100";
			}else {
				startRanking = "100";
			}
			pam.put("startRanking", startRanking);
			pam.put("endRanking", endRanking);
		}		
		
		PageInfo<?> page = pageTool.getAutoPage(keywordGMemberEService::getKeyWordsEList,pam);
		request.setAttribute("page", page);
		request.setAttribute("venderList", venderList);
		request.setAttribute("groupList", groupList);
		request.setAttribute("engineList", engineList);
		request.setAttribute("pam", pam);
		return "keyWord/keyWordE_list";

	}
	
	/**
	 * 导出
	 * @param pam
	 * @param response
	 */
	@RequestMapping("export")
	public void exportKeyWord(@RequestParam Map<String, Object> pam,HttpServletResponse response){
		XSSFWorkbook workbook = new XSSFWorkbook();
		try {
			Integer userId = Jurisdiction.getT("id", Integer.class);
			pam.put("userId", userId);
			workbook = keywordGMemberEService.exportKeyWord(pam, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 下级关键词列表
	 * @param pam
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/lowerlist")
	public String lowerlist(@RequestParam Map<String, Object> pam,HttpServletRequest request, HttpSession session) {
		log.info("进入下级关键词列表...");
		List<Map<String, Object>> venderList = keywordGMemberService.getVenderList(); //厂商列表
		List<Map<String, Object>> engineList = keywordGService.getEngineList(); //搜索引擎列表
		Integer userId = 0;
		if(pam.containsKey("userId") ) {
			String userIdStr = (String) pam.get("userId");
			if(StringUtils.isNotBlank(userIdStr)) {
				userId = Integer.valueOf(userIdStr);
			}else {
				userId = Jurisdiction.getT("id", Integer.class);
			}
		}else {
			userId = Jurisdiction.getT("id", Integer.class);
		}
		List<Map<String, Object>> groupList =  keywordGMemberService.getGroupByUserId(userId); //所在分组
		pam.put("parentId", userId);
		//排名区间 1：1-10 2：11-50 3： 51-100 4: 100+
		String rankingInterval = (String) pam.get("rankingInterval");
		if(StringUtils.isNotBlank(rankingInterval)) {
			String startRanking = null;
			String endRanking = null;
			if("1".equals(rankingInterval)) {
				startRanking = "1";
				endRanking = "10";
			}else if("2".equals(rankingInterval)) {
				startRanking = "11";
				endRanking = "50";
			}else if("3".equals(rankingInterval)) {
				startRanking = "51";
				endRanking = "100";
			}else {
				startRanking = "100";
			}
			pam.put("startRanking", startRanking);
			pam.put("endRanking", endRanking);
		}		
		
		PageInfo<?> page = pageTool.getAutoPage(keywordGMemberEService::getKeyWordsEList,pam);
		request.setAttribute("page", page);
		request.setAttribute("venderList", venderList);
		request.setAttribute("groupList", groupList);
		request.setAttribute("engineList", engineList);
		request.setAttribute("pam", pam);
		return "keyWord/keyWordEChild_list";

	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public ReturnBody delete(@RequestParam Map<String, Object> pam, HttpSession session) {
		log.info("进入关键词删除方法...");
		pam.put("userId", Jurisdiction.getT("id", Integer.class));
		return keywordGService.deleteArrByDay(pam);
		

	}
}
